//
//  STRegisterFinishView.swift
//  iPhone
//
//  Created by Toj on 4/24/23.
//

import UIKit

extension STRegisterFinishView {
    static let registerFinishedNotification = Notification.Name("registerFinishedNotification")
}

class STRegisterFinishView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
        setupMakeInitialize()
        setupMakeLayoutSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /* 背景View */
    private lazy var backgroundView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.black.tt_alpha(0.5)
        
        let tap = UITapGestureRecognizer()
        tap.addTarget(self, action: #selector(didTapBackground))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    /* 展示内容 */
    private lazy var contentView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        view.cm_corner(radius: 24, corners: [.layerMinXMinYCorner, .layerMaxXMinYCorner])
        
        let tap = UITapGestureRecognizer()
        tap.addTarget(self, action: #selector(didTapEndEditing))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    /* 结果图标 */
    private lazy var resultIcon: UIImageView = {
        let view = UIImageView()
        view.contentMode = .center
        return view
    }()
    
    /* 标题 */
    private lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.textColor = .black
        view.font = 20.to_boldFont
        view.textAlignment = .center
        view.numberOfLines = 0
        view.text = "NewSignIn_FinishRegistering".localized()
        return view
    }()

    /* 副标题 */
    private lazy var subtitleLabel: UILabel = {
        let view = UILabel()
        view.textColor = "#666666".to_color
        view.font = 14.to_boldFont
        view.textAlignment = .center
        view.numberOfLines = 0
        view.text = "NewSignIn_FinishRegistering_Welcome".localized()
        return view
    }()
   
    /* ok按钮 */
    private lazy var okButton: UIButton = {
        let view = UIButton()
        view.backgroundColor = "#0087EB".to_color
        view.titleLabel?.font = 14.to_boldFont
        view.setTitleColor(.white, for: .normal)
        view.setTitle("OK".localized(), for: .normal)
        let width = CGFloat.sr_width - 80
        view.frame = CGRect(origin: .zero, size: CGSize(width: width, height: 32.ui_scale))
        view.addTarget(self, action: #selector(didTapOK), for: .touchUpInside)
        view.cm_cornerRadius = 16.ui_scale
        return view
    }()
}

// MARK: - 点击事件
@objc private extension STRegisterFinishView {
    
    func didTapOK() {
        print("-->toj: func:" + #function)
        dismiss()
        
        NotificationCenter.default.post(name: STRegisterFinishView.registerFinishedNotification, object: nil)
    }
    
    func didTapBackground() {
        didTapEndEditing()
        dismiss()
    }
    
    func didTapEndEditing() {
        endEditing(true)
    }
}

// MARK: - Public
extension STRegisterFinishView {
    
    func show() {
        isHidden = false
        setNeedsLayout()
    }
    
    func dismiss() {
        isHidden = true
        removeFromSuperview()
    }
}

// MARK: - 初始化设置
private extension STRegisterFinishView {
    
    func setupMakeAddSubviews() {
        
        addSubview(backgroundView)
        
        addSubview(contentView)
        
        // 结果图标
        contentView.addSubview(resultIcon)
        // 标题
        contentView.addSubview(titleLabel)
        // 副标题
        contentView.addSubview(subtitleLabel)
        
        // ok
        contentView.addSubview(okButton)
    }
    
    func setupMakeLayoutSubviews() {
        
        backgroundView.snp.makeConstraints { make in
            make.edges.equalTo(self)
        }
        
        contentView.snp.remakeConstraints { make in
            make.bottom.left.right.equalTo(self)
            make.height.equalTo(336.ui_mscale)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.right.equalTo(contentView).inset(20)
            make.top.equalTo(100)
        }
        resultIcon.snp.makeConstraints { make in
            make.centerX.top.equalTo(contentView)
            make.width.equalTo(48)
            make.top.equalTo(contentView)
            make.bottom.equalTo(titleLabel.snp.top)
        }
        
        subtitleLabel.snp.makeConstraints { make in
            make.left.right.equalTo(contentView).inset(20)
            make.top.equalTo(titleLabel.snp.bottom).offset(24)
        }
        
        // NEXT
        let size = okButton.frame.size
        okButton.snp.remakeConstraints { make in
            make.centerX.equalTo(contentView)
            make.top.equalTo(subtitleLabel.snp.bottom).offset(40.ui_mscale)
            make.width.equalTo(size.width)
            make.height.equalTo(size.height)
        }
    }
    
    func setupMakeInitialize() {
        let isSuccess = arc4random_uniform(2) == 1
        let imageName = isSuccess ? "signin_register_success_green" : "signin_register_failed_red"
        resultIcon.image = imageName.img_resource
    }
}
